package func.hackathon.c2.yao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by chenyao on 2017/7/24.
 */
public class Loop {

    //存储节点
    private List<Shard> shards;

    public Loop(List<Shard> shards) {
        List<Shard> dest = new ArrayList<>();
        Collections.copy(dest, shards);
        Collections.sort(dest, (s1, s2) -> s1.hashCode() - s2.hashCode());
        this.shards = dest;
    }


    /**
     * 根据key 找到存储节点
     * @param key
     * @return
     */
    private Shard getShard(Object key) {
        int key_hashCode = key.hashCode();
        Shard shardOfSelected = shards.stream()
                .filter(shard -> key_hashCode < shard.hashCode())
                .findFirst().get();
        return shardOfSelected;
    }





}
